package org.lzy.shop.adapter

import android.content.Context
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.QuickViewHolder
import org.lzy.shop.R
import org.lzy.shop.response.UserCoupon

class CouponsHomeAdapter(totalMoney: Float) : BaseQuickAdapter<UserCoupon, QuickViewHolder>()  {
    var totalMoney: Float? =null//外部传个总价来判断
    init {
        this.totalMoney=totalMoney
    }

    override fun onCreateViewHolder(context: Context, parent: ViewGroup, viewType: Int): QuickViewHolder {
        return QuickViewHolder(R.layout.item_coupon_list, parent)
    }

    override fun onBindViewHolder(holder: QuickViewHolder, position: Int, item: UserCoupon?) {
        item?.let {
            holder.setText(R.id.coupon_title, item.title)
            holder.setText(R.id.endTime, "有效期至: "+ item.end_time)
            holder.setText(R.id.amount, "￥ "+ item.amount.toString())
            holder.setText(R.id.fullamount, "满 "+ item.`condition$1`.fullAmount +"元可用")
            if (item.useStatus == 1) {
                holder.itemView.isEnabled=true
                holder.setTextColor(R.id.coupon_title,getResColor(R.color.OnSurface))
                holder.setTextColor(R.id.endTime,getResColor(R.color.OnSurface))
                holder.setTextColor(R.id.amount,getResColor(R.color.colorError))
                holder.setTextColor(R.id.fullamount,getResColor(R.color.OnSurface))
                holder.setGone(R.id.get_now,true)
                holder.setGone(R.id.user_coupon,false)
                holder.setGone(R.id.unuser,true)

            }else if (item.useStatus == 0) {
                val showCouponPrice = item.`condition$1`.fullAmount.toInt()
                //当商品总价不符合优惠劵时候右下角显示不符合条件，符合显示立即获取
                totalMoney?.let { it1 ->
                    if (showCouponPrice < it1) {
                        holder.itemView.isEnabled=false
                        holder.setTextColor(R.id.coupon_title,getResColor(R.color.OnSurface))
                        holder.setTextColor(R.id.endTime,getResColor(R.color.OnSurface))
                        holder.setTextColor(R.id.amount,getResColor(R.color.colorError))
                        holder.setTextColor(R.id.fullamount,getResColor(R.color.OnSurface))
                        holder.setGone(R.id.unuser,true)
                        holder.setGone(R.id.get_now,false)
                        holder.setGone(R.id.user_coupon,true)
                    }else{
                        holder.itemView.isEnabled=false
                        holder.setTextColor(R.id.coupon_title,getResColor(R.color.OnSurface))
                        holder.setTextColor(R.id.endTime,getResColor(R.color.OnSurface))
                        holder.setTextColor(R.id.amount,getResColor(R.color.colorError))
                        holder.setTextColor(R.id.fullamount,getResColor(R.color.OnSurface))
                        holder.setGone(R.id.unuser,false)
                        holder.setGone(R.id.get_now,true)
                        holder.setGone(R.id.user_coupon,true)
                    }
                }



            }


        }

    }

    fun getResColor(resid: Int): Int {
        return ContextCompat.getColor(context, resid)
    }
}